package com.itsu.admin.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.itsu.admin.constant.Constant;
import com.itsu.admin.exception.BizException;
import com.itsu.admin.mvc.Result;
import com.itsu.admin.service.MainPageService;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.nio.charset.StandardCharsets;

/**
 * @author Jerry.su
 * @Date 2022/1/2 15:42
 */
@Service
public class MainPageServiceImpl implements MainPageService {


    @Override
    public Result getNavs() {
        try {
            File file = ResourceUtils.getFile("classpath:data/navs.json");
            String jsonStr = FileUtil.readString(file, StandardCharsets.UTF_8);
            JSONArray jsonArray = JSONUtil.parseArray(jsonStr);
            return Result.ok(jsonArray);
        } catch (Exception e) {
            throw new BizException(e, Constant.BIZ_CODE);
        }
    }
}
